* Simulate bias measure based on random electiondecision;
#delimit ;

g retention = electiondecision; replace extv = round(extv); set seed 010101;
forval m = 1/10 {;
tempname sim; postfile `sim' sessub it bias diffarea overlap1 overlap0
using  `data'/01_descriptives/sim`m', replace;
forval n = 1/100 {; di "round: `m', iteration: `n'"; qui {; 
	forval in = 0/1 {;
		g rarea`in' = .; g norarea`in' = .; g meanarea`in' = .; 
		g overlap`in' = .;
		vallist sessub; 
		foreach s in `r(list)' {;
			preserve; keep if(sessub==`s'&ingroup==`in');
			replace election = rbinomial(1,.5);
			local obs = 61*16; set obs `obs'; egen extvval = seq(), from(5) 
			to(65); 
			bysort extvval: egen c1val = seq(), from(5) to(20);  
			forval r = 0/1 {;
				if(`r'==0){; 
					local v "nor"; local sign1 "<="; local sign2 ">="; 
					};
				else {; 
					local v "r"; local sign1 ">="; local sign2 "<="; 
					};
				g `v'extvindex = .; g `v'index = 0; local i = 1; 
				gsort -extv; vallist extv if(election==`r');  
				foreach extv in `r(list)' {;
					g `v'c1index`i' = .;
					vallist c1 if(extv==`extv'&election==`r');
					g `v'extv`i' = `extv'; local j = 1;
					foreach c1 in `r(list)' {; 
						g `v'c1`i'_`j' = `c1'; local j = `j'+1;
						};
					replace `v'c1index`i' = `j'-1; local i = `i'+1;
					};
				replace `v'extvindex = `i'-1; sum `v'extvindex; 
				local max = r(max); 
				forval i = 1/`max' {;
					sum `v'c1index`i'; 
					forval j = 1/`r(max)' {; 
						if(`r'==0){; 
							local extvmax = 65; local c1max = 20; 
							};
						else {; 
							local extvmax = 0; local c1max = 0; 
							};
						sum `v'extv`i'; sum `v'c1`i'_`j'; sum extvval; 
						replace `v'index = 1 
						if(extvval`sign1'`v'extv`i'&c1val`sign1'`v'c1`i'_`j'&
						(`extvmax'`sign2'`v'extv`i'|`c1max'`sign2'`v'c1`i'_`j'));
						sum extvval if(`v'index==1); local extvmax = r(max); 
						sum c1val if(`v'index==1); local c1max = r(max);
						};
					};
				};	
			g y = .; g ymaxnor = .; g yminr = .; g xmaxnor = .; g xminr = .;
			g x = extvval; g nory = .; g ry = .;
			forval extv = 5(1)65 {;
				sum c1val if(extvval==`extv'&norindex==1);
				replace nory = r(max) if(extvval==`extv');
				sum c1val if(extvval==`extv'&rindex==1);
				replace ry = r(min) if(extvval==`extv');
				};
			forval c1 = 5(1)20 {;
				sum extvval if(c1val==`c1'&norindex==1);
				replace nory = `c1' if(extvval==r(max)&c1val==`c1');
				sum extvval if(c1val==`c1'&rindex==1);
				replace ry = `c1' if(extvval==r(min)&c1val==`c1');
				};	
			count if(rindex==1); local rarea = r(N); 
			count if(norindex==1); local norarea = r(N);
			count if(rindex==1&norindex==1); local overlap = r(N);
			restore; 
			replace rarea`in' = `rarea' if(sessub==`s'); 
			replace norarea`in' = `norarea' if(sessub==`s'); 	
			replace overlap`in' = `overlap' if(sessub==`s');
			};
		};
	g area = 61*16; 
	replace meanarea1 = (area - (norarea1+rarea1))/2 if(overlap1==0);
	replace meanarea0 = (area - (norarea0+rarea0))/2 if(overlap0==0);
	replace meanarea1 = (area - (norarea1+rarea1) + overlap1)/2 
	if(overlap1~=0);
	replace meanarea0 = (area - (norarea0+rarea0) + overlap0)/2 
	if(overlap0~=0);
	g area1 = area-(norarea1+meanarea1);
	g area0 = area-(norarea0+meanarea0);
	g diffarea = area1 - area0;
	g bias = 1 if(diffarea>0); replace bias = -1 if(diffarea<0);
	replace bias = 0 if(diffarea==.);
	vallist sessub;
	foreach s in `r(list)' {;
		sum bias if(sessub==`s'); local bias = r(mean);
		sum diffarea if(sessub==`s'); local diff = r(mean);
		sum overlap1 if(sessub==`s'); local overlap1 = r(mean);
		sum overlap0 if(sessub==`s'); 
		post `sim' (`s') (`n') (`bias') (`diff') (`overlap1') (r(mean));
		};
	drop area area* mean* rarea* norarea* bias overlap* diffarea;	
	}; 
};	
postclose `sim';
};

use `data'/01_descriptives/sim1, clear; g round = 1;
forval i = 2/10 {;
	append using `data'/01_descriptives/sim`i'; 
	replace round = `i' if(round==.);
	};	
	
g prop1 = .; g prop0 = .;
forval i = 1/10 {;
	forval j = 1/100 {;
		di "round:`i', iteration:`j'";
		qui {;
			count if(round==`i'&it==`j'); local N = r(N);
			forval o = 0/1 {;
			 	count if(round==`i'&it==`j'&overlap`o'==0); 
			 	replace prop`o' = r(N)/`N' if(round==`i'&it==`j');
				};
			};
		};		
	};